package com.my.mvvmrapid.ui.main.fragment.homewan;

import android.app.Application;

import androidx.annotation.NonNull;

import com.my.mvvmrapid.data.http.ArticleDataSourceImpl;
import com.my.mvvmrapid.data.http.BannerDataSourceImpl;
import com.my.mvvmrapid.data.http.HotkeyDataSourceImpl;
import com.my.mvvmrapid.data.http.retrofit.Rxjava2Manage;
import com.my.mvvmrapid.entity.ArticleTop;
import com.my.mvvmrapid.entity.Banner;
import com.my.mvvmrapid.entity.Hotkey;
import com.my.mvvmrapid.entity.WanEntity;
import com.zhongjh.mvvmrapid.base.viewmodel.BaseViewModel;
import com.zhongjh.mvvmrapid.bus.event.SingleLiveEvent;
import com.zhongjh.mvvmrapid.http.ResponseThrowable;
import com.zhongjh.mvvmrapid.http.RxUtils;
import com.zhongjh.mvvmrapid.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 *
 * @author zhongjh
 * @date 2021/4/8
 */
public class HomeWanViewModel extends BaseViewModel {

    BannerDataSourceImpl mBannerDataSourceImpl = new BannerDataSourceImpl();
    HotkeyDataSourceImpl mHotKeyDataSourceImpl = new HotkeyDataSourceImpl();
    ArticleDataSourceImpl mArticleDataSourceImpl = new ArticleDataSourceImpl();

    /*** 界面发生改变的观察者 */
    public UiChangeObservable uiChangeObservable = new UiChangeObservable();

    public static class UiChangeObservable {
        // 显示banner
        public SingleLiveEvent<List<Banner>> showBanners = new SingleLiveEvent<>();
        // 显示topLine
        public SingleLiveEvent<List<Hotkey>> showHotKeys = new SingleLiveEvent<>();
        // 显示置顶文章
        public SingleLiveEvent<List<ArticleTop>> showTopJson = new SingleLiveEvent<>();
    }

    public HomeWanViewModel(@NonNull Application application) {
        super(application);
        init();
    }

    public void init() {
        initBanner();
        initTopLine();
        initTopJson();
    }

    /**
     * 初始化banner
     */
    private void initBanner() {
        Rxjava2Manage.simplifyConnection(mBannerDataSourceImpl.json(), this,
                new Rxjava2Manage.CustomObserver<List<Banner>>() {
                    @Override
                    public void onNext(WanEntity<List<Banner>> banners) {
                        // 显示banners
                        uiChangeObservable.showBanners.setValue(banners.getData());
                    }
                });
    }

    /**
     * 初始化头条，即是最多搜索关键字
     */
    private void initTopLine() {
        Rxjava2Manage.simplifyConnection(mHotKeyDataSourceImpl.json(), this,
                new Rxjava2Manage.CustomObserver<List<Hotkey>>() {
                    @Override
                    public void onNext(WanEntity<List<Hotkey>> hotKeys) {
                        // 显示banners
                        uiChangeObservable.showHotKeys.setValue(hotKeys.getData());
                    }
                });
    }

    /**
     * 初始化置顶文章,获取3次接口叠加数据，是一个嵌套获取接口的例子
     */
    private void initTopJson() {
        List<ArticleTop> articleTops = new ArrayList<>();
        mArticleDataSourceImpl.topJson()
                .observeOn(Schedulers.io())
                // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .compose(RxUtils.exceptionTransformer())
                .flatMap((Function<WanEntity<List<ArticleTop>>, ObservableSource<WanEntity<List<ArticleTop>>>>) listWanEntity -> {
                    if (listWanEntity.getCode() != -1) {
                        articleTops.addAll(listWanEntity.getData());
                    }
                    return mArticleDataSourceImpl.topJson();
                })
                .observeOn(Schedulers.io())
                // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .compose(RxUtils.exceptionTransformer())
                .flatMap((Function<WanEntity<List<ArticleTop>>, ObservableSource<WanEntity<List<ArticleTop>>>>) listWanEntity -> {
                    if (listWanEntity.getCode() != -1) {
                        articleTops.addAll(listWanEntity.getData());
                    }
                    return mArticleDataSourceImpl.topJson();
                })
                // 线程调度
                .compose(RxUtils.io2main())
                // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .compose(RxUtils.exceptionTransformer())
//                // 请求与ViewModel周期同步
//                .doOnSubscribe(this)
                .subscribe(new DefaultObserver<WanEntity<List<ArticleTop>>>() {

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull WanEntity<List<ArticleTop>> listWanEntity) {
                        if (listWanEntity.getCode() != -1) {
                            articleTops.addAll(listWanEntity.getData());
                            // 显示置顶文章
                            uiChangeObservable.showTopJson.setValue(articleTops);
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
